package com.caishi.lkx.user.service.impl;

import com.caishi.lkx.user.model.re.SysGroupLocationReModel;
import com.caishi.lkx.user.service.IGroupLocationReService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.IBSMapper;
import com.zzw.common.exception.BizRuntimeException;
import com.caishi.lkx.user.UserResultCode;
import com.caishi.lkx.user.mapper.re.SysGroupLocationReMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class GroupLocationReServiceImpl implements IGroupLocationReService {
    @Resource
    private SysGroupLocationReMapper groupLocationReMapper;


    @Override
    public IBSMapper<SysGroupLocationReModel, Long> getMapper() {
        return groupLocationReMapper;
    }


    @Override
    public void checkExist(BigDecimal longitude, BigDecimal latitude,Long oldId) {
        if (this.selectCount(Wrappers.<SysGroupLocationReModel>lambdaQuery().eq(SysGroupLocationReModel::getLongitude, longitude)
                .eq(SysGroupLocationReModel::getLatitude, latitude)
                .ne(null != oldId,SysGroupLocationReModel::getId,oldId)) > 0) {
            throw new BizRuntimeException(UserResultCode.dataExist);
        }
    }

    @Override
    public Map<String, Object> batchCheckLocationExist(String locationStr) {
        Map<String,Object> map = new HashMap<>();
        List<String> successList = Collections.synchronizedList(new ArrayList<>());
        List<String> failList = Collections.synchronizedList(new ArrayList<>());
        map.put("success",successList);
        map.put("fail",failList);
        String s = locationStr.replaceAll("\r\n|\t|\r|\n", ";");
        String[] split = s.split(";");
        Arrays.stream(split).parallel().forEach( str ->{
            try {
                String[] split1 = str.split("-");
                this.checkExist(new BigDecimal(split1[0].trim()),new BigDecimal(split1[1].trim()),null);
                successList.add(str);
            }catch (Exception e){
                failList.add(str);
            }

        });
        return map;
    }

    @Override
    public Boolean reBind(Long id, Long groupId, String currentUserId) {
        SysGroupLocationReModel build = SysGroupLocationReModel.builder().groupId(groupId).bindTime(LocalDateTime.now()).bindUserId(currentUserId).build();
        build.setId(id);
        this.simpleUpdate(build);
        return true;
    }

    @Override
    public Boolean unBind(Long id, String currentUserId) {
        this.update(null, Wrappers.<SysGroupLocationReModel>lambdaUpdate().eq(SysGroupLocationReModel::getId, id)
                .setSql(" group_id = null , bind_time = null , bind_user_id = '" + currentUserId +"' "));
        return true;
    }

    @Override
    public void deleteByGroupId(Long groupId) {
        this.delete(Wrappers.<SysGroupLocationReModel>lambdaUpdate().eq(SysGroupLocationReModel::getGroupId, groupId));

    }

    @Override
    public void deleteByGroupIds(List<Long> groupIds) {
        this.delete(Wrappers.<SysGroupLocationReModel>lambdaUpdate().in(SysGroupLocationReModel::getGroupId, groupIds));
    }

    @Override
    public Long getGroupIdByLocation(BigDecimal longitude, BigDecimal latitude, Integer groupDistance) {
        if(null == longitude || null == latitude){
            return null;
        }
        SysGroupLocationReModel groupByLocation = groupLocationReMapper.getGroupByLocation(longitude, latitude, groupDistance);
        return null == groupByLocation ? null : groupByLocation.getGroupId();
    }

}
